//
//  SISSComboBoxViewNewHouse.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/11/26.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSComboBoxViewNewHouse.h"

@interface SISSComboBoxViewNewHouse ()<MMComBoBoxViewDataSource, MMComBoBoxViewDelegate>

@property (nonatomic, strong) NSArray *mutableArray;

@end
@implementation SISSComboBoxViewNewHouse


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    self.comBoBoxView = [[MMComBoBoxView alloc] initWithFrame:self.bounds];
    self.comBoBoxView.dataSource = self;
    self.comBoBoxView.delegate = self;
    [self addSubview:self.comBoBoxView];
    [self.comBoBoxView reload];
}



#pragma mark - MMComBoBoxViewDataSource
- (NSUInteger)numberOfColumnsIncomBoBoxView :(MMComBoBoxView *)comBoBoxView {
    return self.mutableArray.count;
}
- (MMItem *)comBoBoxView:(MMComBoBoxView *)comBoBoxView infomationForColumn:(NSUInteger)column {
    return self.mutableArray[column];
}

#pragma mark - MMComBoBoxViewDelegate
- (void)comBoBoxView:(MMComBoBoxView *)comBoBoxViewd didSelectedItemsPackagingInArray:(NSArray *)array atIndex:(NSUInteger)index {
    MMItem *rootItem = self.mutableArray[index];
    switch (rootItem.displayType) {
        case MMPopupViewDisplayTypeNormal: {
            //拼接选择项
            NSMutableString *title = [NSMutableString string];
            __block MMSelectedPath *selectedPath;
            [array enumerateObjectsUsingBlock:^(MMSelectedPath * path, NSUInteger idx, BOOL * _Nonnull stop) {
                [title appendString:idx?[NSString stringWithFormat:@";%@",[rootItem findTitleBySelectedPath:path]]:[rootItem findTitleBySelectedPath:path]];
                if (idx == 0) {
                    selectedPath = path;
                }
            }];
            
            NSLog(@"当title为%@时，所选字段为 %@",rootItem.title ,title);
            if ([self.delegate respondsToSelector:@selector(comboBoxViewDidSelectDisplayTypeNormalWithRootItemTitle:selectedPath:title:)]) {
                
                [self.delegate comboBoxViewDidSelectDisplayTypeNormalWithRootItemTitle:rootItem.title selectedPath:selectedPath title:title];
            }
            
            break;
        }
            
        case MMPopupViewDisplayTypeMultilayer: {
            
            [array enumerateObjectsUsingBlock:^(MMSelectedPath * path, NSUInteger idx, BOOL * _Nonnull stop) {
                
                // 拿到地区的前一个
                MMItem *firstItem = rootItem.childrenNodes[path.firstPath];
                MMItem *secondItem = rootItem.childrenNodes[path.firstPath].childrenNodes[path.secondPath];
                MMItem *thirdItem = rootItem.childrenNodes[path.firstPath].childrenNodes[path.secondPath].childrenNodes[path.thirdPath];
                NSLog(@" rootItem.title:%@  当firstItem.title为%@时，所选字段secondItem.title为 %@,--thirdItem.title %@",rootItem.title,firstItem.title,secondItem.title,thirdItem.title);
                
                if ([self.delegate respondsToSelector:@selector(comboBoxViewDidSelectDisplayTypeMultilayerWithRootItemTitle:title:subtitle:)]) {
                    
                    [self.delegate comboBoxViewDidSelectDisplayTypeMultilayerWithRootItemTitle:rootItem.title title:secondItem.title subtitle:thirdItem.title];
                }
                if ([self.delegate respondsToSelector:@selector(comboBoxViewDidSelectDisplayTypeMultiThreelayersWithRootItemTitle:firstTitle:secondTitle:thirdTitle:)]) {
                    [self.delegate comboBoxViewDidSelectDisplayTypeMultiThreelayersWithRootItemTitle:rootItem.title firstTitle:firstItem.title secondTitle:secondItem.title thirdTitle:thirdItem.title];
                }
            }];
            
            break;
        }
        case MMPopupViewDisplayTypeFilters:{
            MMCombinationItem *combinationItem = self.mutableArray[index];
            [array enumerateObjectsUsingBlock:^(NSMutableArray*  _Nonnull subArray, NSUInteger idx, BOOL * _Nonnull stop) {
                if (combinationItem.isHasSwitch && idx == 0) {
                    for (MMSelectedPath *path in subArray) {
                        MMAlternativeItem *alternativeItem = combinationItem.alternativeArray[path.firstPath];
                        NSLog(@"当title为: %@ 时，选中状态为: %d",alternativeItem.title,alternativeItem.isSelected);
                        
                    }
                    return;
                }
                
                NSString *title;
                NSMutableArray *subtitles = [NSMutableArray array];
                for (MMSelectedPath *path in subArray) {
                    MMItem *firstItem = rootItem.childrenNodes[path.firstPath];
                    MMItem *secondItem = rootItem.childrenNodes[path.firstPath].childrenNodes[path.secondPath];
                    title = firstItem.title;
                    [subtitles addObject:secondItem.title];
                    //                    [subtitles appendString:[NSString stringWithFormat:@"  %@",secondItem.title]];
                }
                NSLog(@"当title为%@时，所选字段为 %@",title,subtitles);
                if ([self.delegate respondsToSelector:@selector(comboBoxViewDidSelectDisplayTypeFiltersWithTitle:subtitles:)]) {
                    
                    [self.delegate comboBoxViewDidSelectDisplayTypeFiltersWithTitle:title subtitles:subtitles.copy];
                }
            }];
            
            if ([self.delegate respondsToSelector:@selector(comboBoxViewDisplayTypeFiltersConfirmClickAction)]) {
                [self.delegate comboBoxViewDisplayTypeFiltersConfirmClickAction];
            }
            
            break;}
        case MMPopupViewDisplayTypeCustomMore:{
//            SISSCustomMoreItem *customMoreItem = self.mutableArray[index];
            [array enumerateObjectsUsingBlock:^(NSMutableArray*  _Nonnull subArray, NSUInteger idx, BOOL * _Nonnull stop) {
                NSString *title;
                NSMutableArray *subtitles = [NSMutableArray array];
                MMSelectedPath *selectedPath = NULL;
                for (MMSelectedPath *path in subArray) {
                    MMItem *firstItem = rootItem.childrenNodes[path.firstPath];
                    MMItem *secondItem = rootItem.childrenNodes[path.firstPath].childrenNodes[path.secondPath];
                    title = firstItem.title;
                    selectedPath = path;
                    [subtitles addObject:secondItem.title];
                    //                    [subtitles appendString:[NSString stringWithFormat:@"  %@",secondItem.title]];
                }
                NSLog(@"当title为%@时，所选字段为 %@",title,subtitles);
                if ([self.delegate respondsToSelector:@selector(comboBoxViewDidSelectDisplayTypeCustomMoreWithTitle:selectedPath:subtitle:)]) {
                    
                    [self.delegate comboBoxViewDidSelectDisplayTypeCustomMoreWithTitle:title selectedPath:selectedPath subtitle:subtitles.firstObject];
                }
            }];
            
            if ([self.delegate respondsToSelector:@selector(comboBoxViewDisplayTypeFiltersConfirmClickAction)]) {
                [self.delegate comboBoxViewDisplayTypeFiltersConfirmClickAction];
            }
            
            break;}
        default:
            break;
    }
}



- (NSArray *)mutableArray {
    
    
    if (_mutableArray == nil) {
        
        NSMutableArray *mutableArray = [NSMutableArray array];
        
        // 区域
        NSArray *townArr = @[@"全部", @"福田区", @"盐田区", @"罗湖区", @"宝安区", @"南山区", @"龙岗区"];
        MMSingleItem *rootItemTown = [MMSingleItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:@"区域"];
        for (int i = 0; i < townArr.count; i++) {
            MMItem *item = [MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:townArr[i]];
            if (i == 0) {
                item.isSelected = YES;
            }
            [rootItemTown addNode:item];
        }
        
        // 价格
        NSString *priceTitleName = @"价格";
        NSArray *priceArr = @[@"全部", @"7000以下", @"7000-10000", @"10000-15000", @"15000-20000", @"20000-25000", @"25000-30000", @"30000-35000", @"35000-40000", @"40000以上"];
        MMSingleItem *rootItemPrice = [MMSingleItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:priceTitleName];
        for (int i = 0; i < priceArr.count; i++) {
            MMItem *item = [MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:priceArr[i]];
            if (i == 0) {
                item.isSelected = YES;
            }
            [rootItemPrice addNode:item];
        }
        
        // 面积
        NSString *squarTitleName = @"面积";
        NSArray *squarArr = @[@"全部", @"60m²以下", @"60-90m²", @"90-120m²", @"120-144m²", @"144-180m²", @"180m²以上"];
        MMSingleItem *rootItemSquar = [MMSingleItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:squarTitleName];
        for (int i = 0; i < squarArr.count; i++) {
            MMItem *item = [MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:squarArr[i]];
            if (i == 0) {
                item.isSelected = YES;
            }
            [rootItemSquar addNode:item];
        }
        
        // 建筑形式
        NSArray *buildingTypeArr = @[@"全部", @"高层", @"小高层", @"多层", @"洋房"];
        MMSingleItem *rootItemBuildingType = [MMSingleItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:@"建筑形式"];
        //        rootItemHouseType.selectedType = MMPopupViewMultilSeMultiSelection;
        for (int i = 0; i < buildingTypeArr.count; i++) {
            MMItem *item = [MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:buildingTypeArr[i]];
            if (i == 0) {
                item.isSelected = YES;
            }
            [rootItemBuildingType addNode:item];
        }
        
        
        // 更多
        SISSCustomMoreItem *rootItemMore = [SISSCustomMoreItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:@"更多"];
        rootItemMore.displayType = MMPopupViewDisplayTypeCustomMore;
        rootItemMore.selectedType = MMPopupViewMultilSeMultiSelection;
        NSArray *arrMore = @[@{@"title":@"住宅类型",
                               @"array": @[@"全部", @"住宅", @"商铺", @"商业", @"别墅", @"公寓", @"写字楼"]
                               },
                             @{@"title":@"地铁",
                               @"array": @[@"全部", @"1号线(罗宝线)", @"2号线(蛇口线)", @"3号线(龙岗线)", @"4号线(龙华线)", @"5号线(环中线)", @"7号线", @"9号线", @"11号线"]
                               },
                             @{@"title":@"销售状态",
                               @"array": @[@"全部", @"在售", @"认筹"]
                               }];
        for (NSDictionary *itemDict in arrMore) {
            
            MMItem *rootItemMore_A = [MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:itemDict[@"title"]];
            [rootItemMore addNode:rootItemMore_A];
            NSArray *itemArr = itemDict[@"array"];
            for (int i = 0; i < itemArr.count; i++){
                MMItem *rootItemMore_B = [MMItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:itemArr[i]];
                rootItemMore_B.tag = i;
                [rootItemMore_A addNode:rootItemMore_B];
            }
        }
        
        [mutableArray addObject:rootItemTown];
        [mutableArray addObject:rootItemPrice];
        [mutableArray addObject:rootItemSquar];
        [mutableArray addObject:rootItemBuildingType];
        [mutableArray addObject:rootItemMore];
        _mutableArray  = [mutableArray copy];
        
    }
    return _mutableArray;
}

@end
